package com.zs.diveinspringboot.listener

import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor
import org.springframework.boot.context.config.ConfigFileApplicationListener
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
import org.springframework.boot.context.event.ApplicationPreparedEvent
import org.springframework.context.ApplicationEvent
import org.springframework.context.event.SmartApplicationListener
import org.springframework.core.Ordered
import org.springframework.lang.Nullable

/**
 * Before {@ling ConfigFileApplicationListener} 实现
 * @description
 * @author madison
 * @date 2021/6/5 18:23
 *
 */
class BeforeConfigFileApplicationListener : SmartApplicationListener, Ordered {
    override fun onApplicationEvent(event: ApplicationEvent) {
        if (event is ApplicationEnvironmentPreparedEvent) {
            val environment = event.environment
            println("environment.getProperty(\"name\"):${environment.getProperty("name")}")
        }
        if (event is ApplicationPreparedEvent) {

        }
    }

    override fun supportsEventType(eventType: Class<out ApplicationEvent>): Boolean {
        return ApplicationEnvironmentPreparedEvent::class.java.isAssignableFrom(eventType)
                || ApplicationPreparedEvent::class.java.isAssignableFrom(eventType);
    }

    override fun supportsSourceType(@Nullable sourceType: Class<*>?): Boolean {
        return true
    }

    override fun getOrder(): Int {
//        return ConfigFileApplicationListener.DEFAULT_ORDER - 1;
//        return ConfigDataEnvironmentPostProcessor.ORDER - 1
        return ConfigDataEnvironmentPostProcessor.ORDER + 1
    }
}